package com.zhuanzhuan.hero.user.service.impl;

import com.bj58.youpin.common.rpc.result.Result;
import com.bj58.youpin.common.utils.json.JsonUtils;
import com.bj58.zhuanzhuan.zzentry.common.service.entity.ServiceHeaderEntity;
import com.zhuanzhuan.hero.biz.common.contract.IRiskControlFacade;
import com.zhuanzhuan.hero.biz.common.entity.risk.control.request.*;
import com.zhuanzhuan.hero.biz.common.entity.risk.control.response.RiskControlResult;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.enums.RiskControlFieldEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.hero.user.service.IRiskControlService;
import com.zhuanzhuan.hero.user.util.ServiceHeaderUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName RiskControlServiceImpl
 * @Description
 * @Author hanjunjun
 * @Date 2024/6/20 20:27
 */
@Slf4j
@Service
public class RiskControlServiceImpl implements IRiskControlService {

    @Resource
    private IRiskControlFacade riskControlFacade;

    @Override
    public RiskControlResult userBaseInfoRiskControl(Long uid, ServiceHeaderEntity header,
        UserBaseInfoRiskControlRequest userBaseInfoRiskControlRequest) {
        this.buildRiskControlRequestBase(uid, uid, userBaseInfoRiskControlRequest, header);
        log.info("k=s act=userBaseInfoRiskControl param={}", JsonUtils.toJsonWithoutNull(userBaseInfoRiskControlRequest));
        Result<RiskControlResult> result =
            riskControlFacade.userBaseInfoRiskControl(userBaseInfoRiskControlRequest);
        log.info("k=s act=userBaseInfoRiskControl result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("风控接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }
        return result.getData();
    }

    @Override
    public RiskControlResult userOuterInfoRiskControl(Long uid, ServiceHeaderEntity header,
        RecycleShareRiskControlRequest riskControlRequest) {
        this.buildRiskControlRequestBase(uid, uid, riskControlRequest, header);
        log.info("k=s act=recycleShareRiskControl param={}", JsonUtils.toJsonWithoutNull(riskControlRequest));
        Result<RiskControlResult> result =
            riskControlFacade.recycleShareRiskControl(riskControlRequest);
        log.info("k=s act=recycleShareRiskControl result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("风控接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }
        return result.getData();
    }

    @Override
    public RiskControlResult userContactInfoRiskControl(Long uid, ServiceHeaderEntity header,
        UserContactRiskControlRequest userContactRiskControlRequest) {
        this.buildRiskControlRequestBase(uid, uid, userContactRiskControlRequest, header);
        log.info("k=s act=userContactInfoRiskControl param={}", JsonUtils.toJsonWithoutNull(userContactRiskControlRequest));
        Result<RiskControlResult> result =
            riskControlFacade.userContactRiskControl(userContactRiskControlRequest);
        log.info("k=s act=userContactInfoRiskControl result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("风控接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }
        return result.getData();
    }

    @Override
    public RiskControlResult userPersonIntroductionRiskControl(Long uid, ServiceHeaderEntity header,
        UserProfileRiskControlRequest userProfileRiskControlRequest) {
        this.buildRiskControlRequestBase(uid, uid, userProfileRiskControlRequest, header);
        log.info("k=s act=userPersonDescRiskControl param={}", JsonUtils.toJsonWithoutNull(
            userProfileRiskControlRequest));
        Result<RiskControlResult> result =
            riskControlFacade.userProfileRiskControl(userProfileRiskControlRequest);
        log.info("k=s act=userPersonDescRiskControl result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("风控接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }
        return result.getData();
    }

    @Override
    public RiskControlResult fraternityAuthLabelRiskControl(Long uid, Long labelId, ServiceHeaderEntity header,
        FellowCertificateLabelRiskControlRequest riskControlRequest) {
        this.buildRiskControlRequestBase(uid, labelId, riskControlRequest, header);
        log.info("k=s act=fellowCertificateLabelRiskControl param={}", JsonUtils.toJsonWithoutNull(
            riskControlRequest));
        Result<RiskControlResult> result =
            riskControlFacade.fellowCertificateLabelRiskControl(riskControlRequest);
        log.info("k=s act=fellowCertificateLabelRiskControl result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("风控接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }
        return result.getData();
    }

    @Override
    public RiskControlResult userBackdropRiskControl(Long uid, ServiceHeaderEntity header,
        UserCoverRiskControlRequest riskControlRequest) {
        this.buildRiskControlRequestBase(uid, uid, riskControlRequest, header);
        log.info("k=s act=userCoverRiskControl param={}", JsonUtils.toJsonWithoutNull(
            riskControlRequest));
        Result<RiskControlResult> result = riskControlFacade.userCoverRiskControl(riskControlRequest);
        log.info("k=s act=userCoverRiskControl result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("风控接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }
        return result.getData();
    }

    @Override
    public RiskControlResult unReliableRiskControl(Long uid, Long authRecordId, ServiceHeaderEntity header,
        UnreliableCertificateRiskControlRequest riskControlRequest) {
        this.buildRiskControlRequestBase(uid, authRecordId, riskControlRequest, header);
        log.info("k=s act=unreliableCertificateRiskControl param={}", JsonUtils.toJsonWithoutNull(
            riskControlRequest));
        Result<RiskControlResult> result = riskControlFacade.unreliableCertificateRiskControl(riskControlRequest);
        log.info("k=s act=unreliableCertificateRiskControl result={}", JsonUtils.toJsonWithoutNull(result));
        if (!result.isSuccess()) {
            log.error("风控接口调用失败");
            throw new BusinessException(ExceptionMsgEnum.BUSINESS_ERROR);
        }
        return result.getData();
    }

    @Override
    public String parseTitleByField(String field) {
        if (StringUtils.isBlank(field)) {
            return "";
        }
        return RiskControlFieldEnum.getTitleByField(field);
    }

    private void buildRiskControlRequestBase(Long uid, Long entityId, RiskControlRequestBase riskControlRequestBase,
        ServiceHeaderEntity header) {
        riskControlRequestBase.setUid(uid);
        riskControlRequestBase.setEntityId(entityId);
        riskControlRequestBase.setDeviceId(header.getToken());
        riskControlRequestBase.setIp(header.getClientIp());
        riskControlRequestBase.setClientSource(Integer.valueOf(ServiceHeaderUtil.parseTvalue(header)));
    }

}
